package com.logic.modular.game.battle.panel.attribute;

import cn.hutool.core.bean.BeanUtil;
import com.logic.comment.util.RedisUtils;
import com.logic.modular.game.battle.panel.attribute.domain.PlayerHeroAttribute;
import com.comment.pojo.domain.logic.domain.PlayerHero;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import javax.validation.constraints.NotNull;

/**
 * 基础服务-武将属性
 *
 * @author cxy
 * @since 2024-09-24 14:45:31
 */
@Slf4j
@Service
public class PlayerHeroAttributeBaseServiceImpl {
    public static final String PlayerHeroAttribute_key = "heroAttribute:%s";
    public static final String PlayerHeroAttribute_base_key = "heroAttributeBase:%s";
    @Resource
    public RedisUtils redisUtils;

    /**
     * 初始化武将面板
     *
     * @param battleUUID 战斗编号
     * @param actionList 参加武将
     */
    public void init(String battleUUID, List<PlayerHero> actionList) {
        HashSet<PlayerHero> hashSet = new HashSet<>(actionList);
        for (PlayerHero playerHero : hashSet) {
            PlayerHeroAttribute bean = BeanUtil.toBean(playerHero, PlayerHeroAttribute.class);
            bean.setPhase(0);// 战斗中
            bean.setBattleUUID(battleUUID);
            bean.event = new HashMap<>();
            bean.setBehaviorMap(new HashMap<>());
            bean.obtainSkillInfo(playerHero);
            if (bean.getUid() < 1) {
                bean.setId(-bean.getId());
            }

            Long id = bean.getId();
            removeCache(battleUUID, id);// 删除面板进行下一场战斗

            redisUtils.hPut(
                    String.format(PlayerHeroAttribute_key, battleUUID),
                    bean.getId().toString(),
                    bean,
                    20,
                    TimeUnit.MINUTES
            );
            redisUtils.hPut(
                    String.format(PlayerHeroAttribute_base_key, battleUUID),
                    bean.getId().toString(),
                    bean,
                    20,
                    TimeUnit.MINUTES
            );
        }
    }

    ///////////////////////////////////////////////////////////////////////////////////////////////////////////  缓存部分
    public void saveCache(PlayerHeroAttribute entity) {
        if (entity.getCurrentForce() == null) {
            throw new RuntimeException("当前兵力不能为空");
        }
        String key1 = String.format(PlayerHeroAttribute_key, entity.getBattleUUID());
        String key2 = entity.getId().toString();
        redisUtils.hPut(
                key1,
                key2,
                entity,
                20,
                TimeUnit.MINUTES
        );
    }

    public PlayerHeroAttribute selectCache(
            @NotNull(message = "战斗id不能为空") String battleUUID,
            @NotNull(message = "玩家英雄id不能为空") Long playerHeroId
    ) {
        String key1 = String.format(PlayerHeroAttribute_key, battleUUID);
        String key2 = playerHeroId.toString();
        redisUtils.expire(key1, 60, TimeUnit.MINUTES);
        return BeanUtil.toBean(redisUtils.hGet(key1, key2), PlayerHeroAttribute.class);
    }

    public List<PlayerHeroAttribute> listCache(
            String battleUUID

    ) {
        String key1 = String.format(PlayerHeroAttribute_key, battleUUID);
        redisUtils.expire(key1, 60, TimeUnit.MINUTES);
        return redisUtils.hGetAll(key1).values().stream().map(item -> (PlayerHeroAttribute) item).collect(Collectors.toList());
    }

    /**
     * 获取 基础属性
     */
    public List<PlayerHeroAttribute> listBaseCache(
            String battleUUID
    ) {
        String key1 = String.format(PlayerHeroAttribute_base_key, battleUUID);
        redisUtils.expire(key1, 60, TimeUnit.MINUTES);
        return redisUtils.hGetAll(key1).values().stream().map(item -> (PlayerHeroAttribute) item).collect(Collectors.toList());
    }

    public Map<String, PlayerHeroAttribute> mapCache(
            @NotNull(message = "keyValue1不能为空") Long keyValue1
    ) {
        String key1 = String.format(PlayerHeroAttribute_key, keyValue1);
        redisUtils.expire(key1, 60, TimeUnit.MINUTES);
        return redisUtils.hGetAll(key1).values().stream()
                .map(item -> BeanUtil.toBean(item, PlayerHeroAttribute.class))
                .collect(Collectors.toMap(k -> k.getId().toString(), v -> v));
    }

    public void removeCache(
            @NotNull(message = "战斗id不能为空") String battleUUID,
            @NotNull(message = "玩家英雄id不能为空") Long id
    ) {
        String key1 = String.format(PlayerHeroAttribute_key, battleUUID);
        String key2 = id.toString();
        redisUtils.hDelete(key1, key2);
    }

    /**
     * 获取 属性
     */
    public PlayerHeroAttribute get(String battleUUID, Long playerHeroId) {
        PlayerHeroAttribute bean = (PlayerHeroAttribute) redisUtils.hGet(
                String.format(PlayerHeroAttribute_key, battleUUID),
                playerHeroId.toString()
        );
        if (bean == null) {
            throw new RuntimeException("数据不存在");
        }
        return bean;
    }

    /**
     * 获取 基础属性
     */
    public PlayerHeroAttribute getBase(String battleUUID, Long playerHeroId) {
        PlayerHeroAttribute bean = (PlayerHeroAttribute) redisUtils.hGet(
                String.format(PlayerHeroAttribute_base_key, battleUUID),
                playerHeroId.toString()
        );
        if (bean == null) {
            throw new RuntimeException("数据不存在");
        }
        return bean;
    }
}
